package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;

@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {
    /**
     * 注册
     *
     * @param adUser
     * @return
     */
    @Override
    public ResponseResult regist(AdUser adUser) {

        //参数校验
//        if (StringUtils.isBlank(adUser.getName()) || StringUtils.isBlank(adUser.getPassword())) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_NAMEORPSW_ISNOTNULL);
//        }

        //注册用户信息
        //判断用户名是否重复
        AdUser user = getOne(Wrappers.<AdUser>lambdaQuery()
                .eq(AdUser::getName, adUser.getName())
        );
        if (user != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_NAME_EXIST);
        }

        //获取密码加密加盐
        adUser.setSalt("123abc");
        String psw = adUser.getPassword();
        String salt = adUser.getSalt();
        String npsw = DigestUtils.md5DigestAsHex((psw + salt).getBytes());

        adUser.setPassword(npsw);
        adUser.setCreatedTime(new Date());
        adUser.setStatus(9);

        save(adUser);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 登录
     *
     * @param adUserDto
     * @return
     */
    @Override
    public ResponseResult login(AdUserDto adUserDto) {
        //参数校验
        if (StringUtils.isBlank(adUserDto.getName()) || StringUtils.isBlank(adUserDto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_NAMEORPSW_ISNOTNULL);
        }

        //查询用户名
        AdUser adUser = getOne(Wrappers.<AdUser>lambdaQuery().
                eq(AdUser::getName, adUserDto.getName())
                .eq(AdUser::getStatus, 9));

        if (adUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_NAMENOTNORMALORPSW_ERROR);
        }

        //比对密码
        String dbpsw = adUser.getPassword();
        String psw = adUserDto.getPassword();
        String salt = adUser.getSalt();
        String npsw = psw + salt;
        String dpsw = DigestUtils.md5DigestAsHex(npsw.getBytes());
        if (!dbpsw.equals(dpsw)) {
            CustException.cust(AppHttpCodeEnum.ADMIN_NAMEORPSW_ERROR);
        }

        //设置最后一次登录时间
        adUser.setLoginTime(new Date());
        updateById(adUser);

        //根据用户名拿到token,把用户信息存在token中并返回给前端
        String token = AppJwtUtil.getToken(adUser.getId().longValue());
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        adUser.setPassword(null);
        adUser.setPhone(null);
        adUser.setSalt(null);

        map.put("user", adUser);

        return ResponseResult.okResult(map);
    }
}
